package com.polaris.live.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.EncodeUtils
import com.polaris.live.common.AppModule
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.manager.startCountdown
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.resp.UserResp
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.VerifyResp
import com.polaris.live.resp.go_resp.LoginPhoneBean
import com.polaris.live.resp.go_resp.ModifyPasswordBean
import com.polaris.live.resp.go_resp.VerifyBean
import com.polaris.live.viewmodel.login.LoginViewModel

/**
 * 验证码的viewModel
 *
 * @author Created by 半仙 on 2023/8/15/015 17:11
 */
class VerifyViewModel : LoginViewModel() {

    /**
     * 倒计时
     */
    val countdownLiveData = MutableLiveData<Long>()
    fun countdown(timeInMillis: Long = 60 * 1000) {
        viewModelScope.startCountdown(timeInMillis, 1000, {
            countdownLiveData.value = it
        }, {
            countdownLiveData.value = 0
        })
    }

    /**
     * 获取手机验证码
     */
    val verifyLiveData = MutableLiveData<ResultState<VerifyResp>>()
    fun sendPhoneVerify(
        initial: Boolean,
        areaCode: String,
        phoneNumber: String,
        googleValidate: String?,
        userType: Int,
    ) {
        val newPhoneNumber = phoneNumber.replace("\\s+".toRegex(), "")  // 去掉所有空格
        val verifyBean = VerifyBean(
            userSmsType = userType,
            areaCode = areaCode,
            phoneNumber = newPhoneNumber,
            validate = googleValidate
        )

        if (initial) {
            request({ userService.getVerify(verifyBean) }, verifyLiveData)
        } else {
            requestLoading({ userService.getVerify(verifyBean) }, verifyLiveData)
        }
    }

    /**
     * 获取邮箱验证码
     */
    fun sendGmailVerify(
        initial: Boolean,
        gmail: String,
        googleValidate: String?,
        userType: Int,
    ) {
        val newGmail = gmail.replace("\\s+".toRegex(), "")  // 去掉所有空格
        val verifyBean = VerifyBean(
            userSmsType = userType,
            gmail = newGmail,
            validate = googleValidate
        )

        if (initial) {
            request({ userService.getGmailVerify(verifyBean) }, verifyLiveData)
        } else {
            requestLoading({ userService.getGmailVerify(verifyBean) }, verifyLiveData)
        }
    }

    /**
     * 手机号密码登录
     */
    val loginPwsRespLiveData = MutableLiveData<ResultState<UserResp>>()
    fun loginPhoneByPassword(
        areaCode: String,
        phoneNumber: String,
        password: String,
        loginPwdType: Int,
        verificationCode: String? = null
    ) {
        invokeLoginByPassword(
            areaCode = areaCode,
            phoneNumber = phoneNumber,
            password = password,
            loginPwdType = loginPwdType,
            verificationCode = verificationCode,
            isGmailLogin = false
        )
    }

    fun loginGmailByPassword(
        gmail: String,
        password: String,
        loginPwdType: Int,
        verificationCode: String? = null
    ) {
        invokeLoginByPassword(
            gmail = gmail,
            password = password,
            loginPwdType = loginPwdType,
            verificationCode = verificationCode,
            isGmailLogin = true
        )
    }

    private fun invokeLoginByPassword(
        areaCode: String? = null,
        phoneNumber: String? = null,
        gmail: String? = null,
        password: String,
        loginPwdType: Int,
        verificationCode: String? = null,
        isGmailLogin: Boolean
    ) {
        val newAreaCode = areaCode?.replace("\\s+".toRegex(), "")  // 去掉所有空格
        val newPhoneNumber = phoneNumber?.replace("\\s+".toRegex(), "")  // 去掉所有空格
        val newGmail = gmail?.replace("\\s+".toRegex(), "")  // 去掉所有空格
        val newPassword = password.replace("\\s+".toRegex(), "")
        val bean = LoginPhoneBean(
            areaCode = newAreaCode,
            phoneNumber = newPhoneNumber,
            gmail = newGmail,
            password = String(EncodeUtils.base64Encode(newPassword)),
            verificationCode = verificationCode,
            captchaType = if (isGmailLogin) AppConst.UserLoginType.GMAIL else AppConst.UserLoginType.PHONE,
            loginPwdType = loginPwdType,
            appLanguage = AppModule.getLanguageAndArea()
        )

        requestLoading({ userService.loginByPassword(bean) }, loginPwsRespLiveData)
    }

    /**
     * 手机号登录
     */
    val userResp = MutableLiveData<ResultState<UserResp>>()
    fun goPhoneLogin(areaCode: String, phoneNumber: String, verify: String) {
        val newPhoneNumber = phoneNumber.replace("\\s+".toRegex(), "")  // 去掉所有空格

        val loginPhoneBean = LoginPhoneBean(
            areaCode = areaCode,
            phoneNumber = newPhoneNumber,
            verificationCode = verify,
            appLanguage = AppModule.getLanguageAndArea()
        )

        requestLoading({ userService.loginPhone(loginPhoneBean) }, userResp)
    }

    /**
     * 邮箱登录
     */
    fun goGmailLogin(gmail: String, verify: String) {
        val newGmail = gmail.replace("\\s+".toRegex(), "")  // 去掉所有空格

        val loginPhoneBean = LoginPhoneBean(
            gmail = newGmail,
            verificationCode = verify,
            appLanguage = AppModule.getLanguageAndArea()
        )

        requestLoading({ userService.loginGmail(loginPhoneBean) }, userResp)
    }

    /**
     * 绑定手机号
     */
    val bindPhoneLiveData = MutableLiveData<ResultState<Any>>()
    fun bindPhone(areaCode: String, phoneNumber: String, verify: String) {
        val newPhoneNumber = phoneNumber.replace("\\s+".toRegex(), "")  // 去掉所有空格

        val loginPhoneBean = LoginPhoneBean(
            areaCode = areaCode,
            phoneNumber = newPhoneNumber,
            verificationCode = verify
        )

        requestLoading({ userService.bindPhone(loginPhoneBean) }, bindPhoneLiveData)
    }

    /**
     * 绑定邮箱
     */
    fun bindGmail(gmail: String, verify: String) {
        val newGmail = gmail.replace("\\s+".toRegex(), "")  // 去掉所有空格

        val loginPhoneBean = LoginPhoneBean(
            gmail = newGmail,
            verificationCode = verify
        )

        requestLoading({ userService.bindGmail(loginPhoneBean) }, bindPhoneLiveData)
    }

    /**
     * 修改密码
     */
    val changePwdLiveData = MutableLiveData<ResultState<Any>>()
    fun changePwd(isGmailCode: Boolean, password: String, verificationCode: String) {
        requestLoading(
            {
                userService.infoModifyPassword(
                    ModifyPasswordBean(
                        String(EncodeUtils.base64Encode(password)),
                        verificationCode,
                        if (isGmailCode) AppConst.UserLoginType.GMAIL else AppConst.UserLoginType.PHONE
                    )
                )
            },
            changePwdLiveData
        )
    }
}